﻿/* 
   Copyright 2011 LUCA Studios LLC & John Evans

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

namespace LUCAStudios.Silverlight.Controls
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using DragDrop;

    internal class InsertingDropZone : LUCADropZoneBase
    {
        private const double DefaultThickness = 5;
        private const double DefaultHighlightThickness = 2;
        private readonly bool _isHorizontal;
        private readonly Border _backgroundBorder = new Border
        {
            Background =
                new SolidColorBrush(Color.FromArgb(TargetVisualTransparancy, 200, 200, 200)),
            BorderThickness = new Thickness(0),
            //BorderBrush = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0)),
            IsHitTestVisible = false,
            Visibility = Visibility.Collapsed
        };

        private readonly Border _insertingBorderHighlight = new Border
        {
            Background = new SolidColorBrush(Colors.Transparent),
            BorderThickness = new Thickness(1),
            BorderBrush = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0)),
            IsHitTestVisible = false,
            Visibility = Visibility.Collapsed
        };
        private readonly Grid _hotSpot = new Grid
        {
            Background = new SolidColorBrush(Colors.Transparent)
        };


        public InsertingDropZone(IDragDropTarget source, Rect dimensions, bool isHorizontal, Thickness dropZoneOffsets) 
            : base (source, dimensions, dropZoneOffsets)
        {
            _isHorizontal = isHorizontal;
            InitUIProperties();
        }

        private void InitUIProperties()
        {
            var g = DropZoneUI as Grid;
            if (g == null) return;

            g.Background = null;
            g.Children.Add(_backgroundBorder);
            g.Children.Add(_insertingBorderHighlight);
            g.Children.Add(_hotSpot);

            HotSpot = _hotSpot;

            if (_isHorizontal)
            {
                _insertingBorderHighlight.Height = TargetDimensions.Height;
                _insertingBorderHighlight.Width = DefaultHighlightThickness;
                _insertingBorderHighlight.HorizontalAlignment = HorizontalAlignment.Center;
                _insertingBorderHighlight.VerticalAlignment = VerticalAlignment.Stretch;

                _hotSpot.Height = TargetDimensions.Height;
                _hotSpot.Width = DefaultThickness;
                _hotSpot.HorizontalAlignment = HorizontalAlignment.Center;
                _hotSpot.HorizontalAlignment = HorizontalAlignment.Stretch;

                _backgroundBorder.Height = TargetDimensions.Height;
                _backgroundBorder.Width = TargetDimensions.Width;
                _backgroundBorder.HorizontalAlignment = HorizontalAlignment.Center;
                _backgroundBorder.VerticalAlignment = VerticalAlignment.Stretch;
            }
            else
            {
                _insertingBorderHighlight.Height = DefaultHighlightThickness;
                _insertingBorderHighlight.Width = TargetDimensions.Width;
                _insertingBorderHighlight.HorizontalAlignment = HorizontalAlignment.Stretch;
                _insertingBorderHighlight.VerticalAlignment = VerticalAlignment.Center;

                _hotSpot.Height = DefaultThickness;
                _hotSpot.Width = TargetDimensions.Width;
                _hotSpot.HorizontalAlignment = HorizontalAlignment.Stretch;
                _hotSpot.HorizontalAlignment = HorizontalAlignment.Center;

                _backgroundBorder.Height = TargetDimensions.Height;
                _backgroundBorder.Width = TargetDimensions.Width;
                _backgroundBorder.HorizontalAlignment = HorizontalAlignment.Stretch;
                _backgroundBorder.VerticalAlignment = VerticalAlignment.Center;
            }
        }
        protected override void OnDropZoneEntered()
        {
            _insertingBorderHighlight.Visibility = Visibility.Visible;
            _backgroundBorder.Visibility = Visibility.Visible;
        }

        protected override void OnDropZoneLeave()
        {
            _insertingBorderHighlight.Visibility = Visibility.Collapsed;
            _backgroundBorder.Visibility = Visibility.Collapsed;
        }

        protected override void OnDropZoneDropped()
        {
            _insertingBorderHighlight.Visibility = Visibility.Collapsed;
            _backgroundBorder.Visibility = Visibility.Collapsed;
        }
    }
}
